package com.tang.framework.delay.support;


import com.tang.common.utils.IdUtil;
import com.tang.framework.config.SpringApplication;
import com.tang.framework.delay.support.domain.Message;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;

import java.util.concurrent.TimeUnit;

/**
 * @Author tang jun liang
 * @Date 2023/4/20 16:19
 */
public class DelayMessage {

    public <T> void send(String queueName, T body, long time, TimeUnit timeUnit) {
        RedissonClient redissonClient = SpringApplication.getBean(RedissonClient.class);
        RDelayedQueue<Message<T>> delayedQueue = redissonClient.getDelayedQueue(redissonClient.getBlockingQueue(queueName));
        delayedQueue.offer(buildObjectToMessage(body), time, timeUnit);
    }


    public <T> Message<T> buildObjectToMessage(T body) {
        Message<T> message = new Message<>();
        message.setId(IdUtil.generateDelayMessageId());
        message.setRetry(0);
        message.setBody(body);
        return message;
    }
}
